package com.zxy.rabbitmqdemo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate.ReturnCallback;

import com.google.common.base.Charsets;

/**
 * 如果消息没有到exchange,则confirm回调,ack=false
 *
 * 如果消息到达exchange,则confirm回调,ack=true
 * 
 * exchange到queue成功,则不回调return
 * 
 * exchange到queue失败,则回调return(需设置mandatory=true,否则不回回调,消息就丢了)
 * 
 * Created by zxybird on 2018/3/1.
 * 
 * @see <a href="http://blog.csdn.net/qq315737546/article/details/54176560">reference</a>
 */
public class RetryReturnCallback implements ReturnCallback {

  private final Logger LOG = LoggerFactory.getLogger(getClass());

  @Override
  public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {

    String msgContent = new String(message.getBody(), Charsets.UTF_8);
    // warn一下，然后retry
    LOG.warn(
        "RabbitMQ msg route failed, from exchange = {}, routingKey = {}, msgContent = {}, replyCode = {}, replyText = {}",
        exchange, routingKey, msgContent, replyCode, replyText);

  }
}
